package com.jindy.module.machine.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jindy.module.machine.domain.Machine;
import com.jindy.module.machine.domain.MachineRecord;
import com.jindy.module.machine.domain.machinevo.MachineVo;
import com.jindy.module.machine.service.IMachineRecordService;
import com.jindy.module.machine.service.IMachineService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 机器Controller
 *
 * @author ruoyi
 * @date 2022-10-14
 */
@RestController
@RequestMapping("/module/machine")
public class MachineController extends BaseController
{
    @Autowired
    private IMachineService machineService;

    @Autowired
    private IMachineRecordService machineRecordService;

    /**
     * 查询机器列表
     */
    @PreAuthorize("@ss.hasPermi('module:machine:list')")
    @GetMapping("/list")
    public TableDataInfo list(Machine machine)
    {
        startPage();
        List<Machine> list = machineService.selectMachineList(machine);
        return getDataTable(list);
    }

    /**
     * 查询机器在哪些项目中使用过
     */
//    @PreAuthorize("@ss.hasPermi('module:machine:use')")
    @GetMapping("/useList")
    public TableDataInfo useList(MachineRecord machineRecord)
    {
        startPage();
        List<MachineVo> list = machineRecordService.selectMachineUseRecordList(machineRecord);
        return getDataTable(list);
    }

    /**
     * 导出机器列表
     */
    @Log(title = "机器", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('module:machine:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Machine machine)
    {
        List<Machine> list = machineService.selectMachineList(machine);
        ExcelUtil<Machine> util = new ExcelUtil<Machine>(Machine.class);
        util.exportExcel(response, list, "机器数据");
    }

    /**
     * 获取机器详细信息
     */
//    @PreAuthorize("@ss.hasPermi('module:machine:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(machineService.selectMachineById(id));
    }

    /**
     * 新增机器
     */
//    @PreAuthorize("@ss.hasPermi('module:machine:add')")
    @Log(title = "机器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Machine machine)
    {
        return toAjax(machineService.insertMachine(machine));
    }

    /**
     * 修改机器
     */
    @PreAuthorize("@ss.hasPermi('module:machine:update')")
    @Log(title = "机器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Machine machine)
    {
        return toAjax(machineService.updateMachine(machine));
    }

    /**
     * 机器修好后，根据前端传入的机器状态，判断是否改变库存
     */
    @Log(title = "机器", businessType = BusinessType.UPDATE)
    @PutMapping("/put/edit2")
    public AjaxResult edit2(@RequestBody Machine machine)
    {
        return toAjax(machineService.updateMachine2(machine));
    }




}
